import type { IObject } from '@/types/global';

/**
 * @desc:px转vw
 * @param {number} px px值
 * @returns {string} 转换后的vw
 */
export const pxToVw = (px: number): string => {
  const initVw = 100 / 1920;
  return `${px * initVw}vw`;
};

/**
 * @desc:px转vh
 * @param {number} px px值
 * @returns {string} 转换后的vh
 */
export const p2h = (px: number): string => {
  const initVh = 100 / 1080;
  return `${px * initVh}vh`;
};

/**
 * @desc: 生成FormData格式参数
 */
export const formatData = <T>(params: { [key in keyof T]: string }) => {
  const keys = Object.keys(params);
  const formdata = new FormData();
  for (const item of keys) {
    formdata.append(item, params[item]);
  }
  return formdata;
};

/**
 * @desc: 判断是否为空字段 (若要判断是否为空对象/数组,直接使用lodash.isEmpty();)
 */
export const isVoid = (value: any): boolean => {
  return value === '' || value === null || value === void 0;
};

/**
 * @desc:提交表单去掉空字段
 */
export const exceptSpace = (fieldsValue: IObject): IObject => {
  const currentValue = {
    ...fieldsValue,
  };

  Object.keys(currentValue).map(item => {
    if (isVoid(currentValue[item])) {
      delete currentValue[item];
    }
  });

  return currentValue;
};
